package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.domain.system.Permission;
import com.ihrm.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/sys")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 保存
     */
    @RequestMapping(value="/permission",method = RequestMethod.POST)
    public Result save(@RequestBody Map<String,Object> map) throws Exception {
        permissionService.add(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**u
     * 查询所有
     * type :查询全部权限列表type 0：菜单+按钮  1：菜单  2：按钮  3：api
     * enVisibale ： 0：查询所有sass平台的最高权限   1：查询企业的权限
     * pid  ：父id
     */
    @RequestMapping(value="/permission",method = RequestMethod.GET)
    public Result findAll(@RequestParam Map map,int page, int size){
        //完成查询
        List<Permission> list = permissionService.findAll(map);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value="/permission/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) throws CommonException {
        Map<String, Object> map = permissionService.findById(id);
        return new Result(ResultCode.SUCCESS,map);
    }

    /**
     * 修改
     */
    @RequestMapping(value="/permission/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Map<String,Object> map) throws Exception {
        permissionService.update(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 删除user
     */
    @RequestMapping(value="/permission/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) throws CommonException {
        permissionService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
}
